package com.springboot.cloud.sysadmin.organization.rest;

import cn.hutool.core.util.StrUtil;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.utils.StringUtils;
import com.springboot.cloud.sysadmin.organization.entity.form.NoticeForm;
import com.springboot.cloud.sysadmin.organization.entity.form.NoticeQueryForm;
import com.springboot.cloud.sysadmin.organization.entity.param.NoticeQueryParam;
import com.springboot.cloud.sysadmin.organization.entity.po.Notice;
import com.springboot.cloud.sysadmin.organization.service.INoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Set;

/**
 * Created by ZengJian on 2021/2/3 0003 上午 9:22
 */
@RestController
@RequestMapping("/notice")
@Api(tags = "通知")
@Slf4j

public class NoticeController {

    @Autowired
    private   INoticeService iNoticeService;

    @ApiOperation(value = "新增通知数据", notes = "新增一个通知数据")
    @ApiImplicitParam(name = "noticeForm", value = "新增通知数据form表单", required = true, dataType = "NoticeForm")
    @PostMapping
    public Result add(@Valid @RequestBody NoticeForm noticeForm) {
        log.debug("noticeForm:{}", noticeForm);
        if (iNoticeService.add(noticeForm.toPo(Notice.class))) {
            return Result.success("添加通知数据成功");
        } else {
            return Result.error("不能有重复的通知");
        }
    }

    @ApiOperation(value = "删除通知数据")
    @DeleteMapping
    public Result delete(@Valid @RequestBody Set<String> ids) {
        if (StringUtils.isEmpty(ids)) {
            return Result.error("没选中要删除的通知数据");
        } else if (iNoticeService.delete(ids)) {
            return Result.success("删除通知数据成功");
        }
        return Result.error("没选中要删除的通知数据");

    }

    @ApiOperation(value = "修改通知数据", notes = "修改指定通知数据信息")
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable String id, @Valid @RequestBody NoticeForm noticeForm) {
        if (StrUtil.isBlank(id)) {
            return Result.error("id不可为空");
        }
        Notice notice = noticeForm.toPo(Notice.class);
        notice.setId(id);
        if (iNoticeService.update(notice)) {
            return Result.success("修改通知数据成功");
        }
        return Result.error("更新失败");
    }

    @ApiOperation(value = "获取通知数据", notes = "获取指定通知数据信息")
    @GetMapping(value = "/{id}")
    public Result get(@PathVariable String id) {
        if (StrUtil.isBlank(id)) {
            return Result.error("id不可为空");
        }
        if (iNoticeService.get(id)) {
            return Result.success(iNoticeService.getNotice(id));
        }
        return Result.error("查找不到对应值");
    }

    @ApiOperation(value = "获取通知数据", notes = "获取指定通知数据信息")
    @GetMapping(value = "/type/{dictType}")
    public Result getDictType(@PathVariable String dictType) {
        if (StrUtil.isBlank(dictType)) {
            return Result.error("dictType不可为空");
        }
        return Result.success(iNoticeService.getNotice(dictType));
    }

    @ApiOperation(value = "搜索通知数据", notes = "根据条件查询通知数据信息")
    @PostMapping(value = "/conditions")
    public Result search(@Valid @RequestBody NoticeQueryForm noticeQueryForm) {
        if (noticeQueryForm.getCreatedTime() != null && !noticeQueryForm.getCreatedTime().isEmpty()) {
            noticeQueryForm.setCreatedTimeStart(noticeQueryForm.getCreatedTime().get(0));
            noticeQueryForm.setCreatedTimeEnd(noticeQueryForm.getCreatedTime().get(1));
        }
        log.debug("search with dictDataQueryForm:{}", noticeQueryForm);
        return Result.success(iNoticeService.query(noticeQueryForm.getPage(), noticeQueryForm.toParam(NoticeQueryParam.class)));
    }

    @ApiOperation(value = "获取所有通知数据", notes = "获取所有通知数据")
    @GetMapping(value = "/all")
    public Result get() {
        return Result.success(iNoticeService.getAll());
    }

}
